﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
using tf2hudmaker.Properties;

namespace tf2hudmaker
{
    // Class used to manage the user's tf2 directory
    static class DirectoryManager
    {

        // Check the user's current tf2 directory
        public static bool CheckTf2Directory(string tf2directory)
        {
            bool gameexe = File.Exists(tf2directory + "\\hl2.exe");
            bool tffolder = Directory.Exists(tf2directory + "\\tf");

            return gameexe && tffolder;
        }

        public static string CurrentSearch = "";

        // Find the directory path in which the .res files will be exported and write it 
        // into the user's settings, return true if the path has been correctly found.
        // Leave an empty string in "start directory" to search from the root of each drives
        public static bool GetTf2Directory(string startdirectory)
        {
            // If there are no directory to start, this code will get all the drives
            List<string> drivenamelist = new List<string>();
            if (startdirectory == "")
            {
                DriveInfo[] drivelist = DriveInfo.GetDrives();
                for (int j = 0; j < drivelist.Length; j++)
                {
                    DriveInfo d = drivelist[j];
                    if (d.IsReady)
                        drivenamelist.Add(d.Name);
                }
            }

            // String that will get the first directory that contain the token
            string dir = "";
            string token = "SteamApps\\common\\Team Fortress 2";

            if (startdirectory == "")
            {   // Search through the drives
                foreach (string drivepath in drivenamelist)
                {
                    string aux = SearchDirectory(drivepath, token);
                    if (aux != "")
                    {
                        dir = SearchDirectory(drivepath, token);
                        break;
                    }
                }
            }
            else // Search from the starting directory
                dir = SearchDirectory(startdirectory, token);

            // Check the tf2 installation
            if (CheckTf2Directory(dir))
            {
                Settings.Default._tf2path = dir;
                UserSettings.SaveSettings();
                Console.WriteLine("TF2 directory : " + dir);
                return true;
            }
            else
            {
                Console.WriteLine("Not Found");
                return false;
            }
        }

        // Find the first directory that contain "pattern" in his path
        // Return an empty string if not found.
        private static string SearchDirectory(string start, string pattern)
        {
            try
            {
                foreach (string dir in Directory.GetDirectories(start))
                {
                    DirectoryManager.CurrentSearch = dir;
                    if (dir.Contains(pattern))
                        return dir;
                    else
                    {
                        string aux = "";
                        if (!dir.Contains("Windows"))
                            aux = SearchDirectory(dir, pattern);
                        if (aux == "")
                            continue;
                        else
                            return aux;
                    }
                }
            }
            catch
            {
            }

            return "";
        }
    }
}
